using System;
using System.ComponentModel;
using System.Web.UI.WebControls;
using Sedna.Core.Controller;
using Sedna.Core.Controller.Configurations;
using Sedna.Core.Controls.Support;
using Sedna.Core.DataBinding;

namespace Sedna.Core.Controls
{
	public class CompareValidator : System.Web.UI.WebControls.CompareValidator
	{
        private IConfiguration configuration;
        private string configurationId;

        protected IDataBinder binder;

        private String contentPlaceHolderId;

        public String ContentPlaceHolderId
        {
            get { return contentPlaceHolderId; }
            set { contentPlaceHolderId = value; }
        }

        public IConfiguration Configuration
        {
            get { return configuration; }
            set { configuration = value; }
        }

        [Bindable(true), Category("Sedna"), DefaultValue("configuration.default.CompareValidator")]
        public string ConfigurationId
        {
            get { return configurationId; }
            set { configurationId = value; }
        }

		#region Localization Logic

		private string textKey;
		private string errorMessageKey;


		protected override void OnInit(EventArgs e)
		{
            binder = new DataBinder(this, configuration, ExecutionContext.GetCurrent(ClientID));
		    base.OnInit (e);           
		}


		public string TextKey
		{
			get { return textKey; }
			set { textKey = value; }
		}

		public string ErrorMessageKey
		{
			get { return errorMessageKey; }
			set { errorMessageKey = value; }
		}

		#endregion Localization Logic

        public CompareValidator(IConfiguration configuration) : base()
		{
			this.configuration = configuration;
			BuildControlFromConfiguration();
		}

		protected void BuildControlFromConfiguration()
		{
			ID=Configuration.Id;			
			TextKey = configuration.TextKey;
            Enabled = configuration.Enabled;
            ValidationGroup = configuration.ValidationGroup;


            ICompareValidatorConfiguration validatorConfiguration = Configuration as ICompareValidatorConfiguration;
			if (validatorConfiguration == null)
			{
				return;
			}

            if (configuration.CssClass != null)
            {
                this.CssClass = configuration.CssClass;
            }

			
			ErrorMessageKey = validatorConfiguration.ErrorMessageKey;
		    
            ControlToValidate = validatorConfiguration.ControlToValidate;
            ControlToCompare = validatorConfiguration.ControlToCompare;
            ValueToCompare = validatorConfiguration.ValueToCompare;            
            Type = validatorConfiguration.Type;
            Operator = validatorConfiguration.Operator;
            EnableClientScript = validatorConfiguration.EnableClientScript;
		    
		    
			Display = validatorConfiguration.Display;
		}

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            Unbind();
        }

        protected override void OnPreRender(EventArgs e)
        {
            Bind();
            Text = configuration.GetLocalizedPostfixLabel(TextKey, Text);
            ErrorMessage = configuration.GetLocalizedPostfixLabel(ErrorMessageKey, ErrorMessage);            
            base.OnPreRender(e);
        }

        protected virtual void Bind()
        {
            binder.Bind("Enabled");
        }

        protected virtual void Unbind()
        {
            binder.Unbind("Enabled");
        }
	}
}
